﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ETALON
{
    public partial class FormZ : Form
    {
        DataTable df; functionService.prm_city q; bool IsSelectNode;
        string TABLENAME, SCHEMA;

        const string LEV_1 = "lev", COD_2 = "cod", FO_3 = "fo";
        const int iLEV_1 = 0, iCOD_2 = 1, iFO_3 = 2;
        const int df_fields_lev_1 = 1, df_fields_cod_2 = 2, df_fields_name_3 = 3, df_fields_cop_4 = 4, df_fields_fo_5 = 5;


        string currentNode_tag;
        string cutNode_tag, pasteNode_tag; bool IsSelect_ForCut, IsCut;
        modeWhatCutPaste current_mocp;
        string cutNodeParent_tag;
        // Hashtable ht_currentNode;

        //--------------------------------
        // TreeNode nodeCurrent, nodeCut, nodeCurrentParent_forCut;
        TreeNode nodeSelect_as_sourse, nodeSelect_for_cut, nodeSelect_as_adress_for_Paste;
        TreeNode[] a_nodeSelect_for_cut;
        //--------------------------------

        // modeMove currencyModeMove; 
        modeCut currentModeCut;
        int selectSourseNodeIndex;





        public FormZ()
        {
            InitializeComponent(); InitializeComponentMy();
        }
        void InitializeComponentMy()
        {
            SCHEMA = "city";//schemaName;
            TABLENAME = "zakaz";//tableName;
            q = new functionService.prm_city(); q.con.pass =  q.tk(param.pass_inServ_55); q.con.co(q.tk(param.server_1));

            df = q.selectAny(functionService.shorts.a("call `city`.`select_treea`('", SCHEMA, "', '", TABLENAME, "');")); //q.sh(df);
            df_load();
            IsSelectNode = false;
            currentNode_tag = "0;0;0"; 

        }
        void df_load()
        {
            if (df.Rows.Count > 0)
            {
                readTableParent_readTableChild_0_lev(treeView1, df);
            }
        }
        private void readTableParent_readTableChild_0_lev(TreeView tv, DataTable dfo)
        {
            int levo = 0;
            DataView p = new DataView(df); p.RowFilter = functionService.shorts.a("lev=", levo);
            DataView c = new DataView(df); c.RowFilter = functionService.shorts.a("lev=", levo + 1);
            string at = "";
            int ip = p.Count, ic = c.Count;

            TreeNode nn, nnn;

            for (int i = 0; i < ip; i++)
            {
                //nn = new TreeNode(q.name_cod(p[i][3], p[i][2]));
                nn = new TreeNode(  p[i][3].ToString().Replace("z", "Заказ"));
                c.RowFilter = string.Concat("cop = ", p[i][2], " and lev =", levo + 1);
                for (int j = 0; j < c.Count; j++)
                {
                    at = functionService.shorts.a(c[j][df_fields_lev_1], ";", c[j][df_fields_cod_2], ";", c[j][df_fields_fo_5]);
                    // nnn = new TreeNode(q.name_cod(c[j][df_fields_name_3], c[j][df_fields_cod_2])); nnn.Tag = at; //.Replace("p", "место")
                    nnn = new TreeNode(c[j][df_fields_name_3].ToString().Replace("z", "Заказ")); nnn.Tag = at;
                    nn.Nodes.Add(nnn);
                }
                at = functionService.shorts.a(p[i][df_fields_lev_1], ";", p[i][df_fields_cod_2], ";", p[i][df_fields_fo_5]); nn.Tag = at;
                tv.Nodes.Add(nn);
            }
        }


        void afterExp()
        {
            if (IsSelectNode)
            {

                // selectSourseNodeIndex = treeView1.SelectedNode.Index;fs.shorts.m(selectSourseNodeIndex);

               // setModeCut(modeCut.selectSourse);
                string ag;
                int indexsel = treeView1.SelectedNode.Index;
                string st = treeView1.SelectedNode.Text;

                ag = treeView1.SelectedNode.Tag.ToString();
                int lev = Convert.ToInt32(readTAG(ag, iLEV_1));
                object oc = readTAG(ag, iCOD_2);

                #region LOAD---

                Level_any_readTableParent_readTableChild
                    (
                    treeView1.SelectedNode, indexsel, oc,
                    lev
                    );
                #endregion

                IsSelectNode = false;
            }
        }
        private void Level_any_readTableParent_readTableChild(TreeNode tno,
       int indexSelectNode, object objectCodParent, int levo)
        {
            DataView p = new DataView(df);
            p.RowFilter = string.Concat("cop = ", objectCodParent, " and lev =", levo + 1);

            int ip = p.Count; //fs.shorts.m(fs.shorts.aa(ip, "=ip"));

            //------------------------------------------------------folder cut
           // if (ip > 0) setModeCut(modeCut.selectSourse);
            //------------------------------------------------------

            DataView c = new DataView(df); //c.RowFilter = string.Concat("cop = ", objectCodParent, " and lev =", levo);
            TreeNode nn, nnn;
            string ag = "";
            //Hashtable hta = new Hashtable(); setHTA(hta, 0,0,0);

            tno.Nodes.Clear();
            for (int i = 0; i < ip; i++)
            {
                //  setHTA(hta, p[i][iLEV_1], p[i][iCOD_2], p[i][iFO_3]);


                nn = new TreeNode(p[i][3].ToString().Replace("z", "Заказ").Replace("p","Место"));//q.name_cod(p[i][3], p[i][2])
                c.RowFilter = string.Concat("cop = ", p[i][2], " and lev = ", levo + 2);

                for (int j = 0; j < c.Count; j++)
                {
                    //setHTA(hta, c[j][iLEV_1], c[j][iCOD_2], c[j][iFO_3]);
                    ag = functionService.shorts.a(c[j][1], ";", c[j][2], ";", c[j][5]);
                    nnn = new TreeNode(functionService.nameCod.name_cod(c[j][3], c[j][2])); nnn.Tag = ag; nnn.ImageIndex = imageIndex(c[j][5], true); nnn.SelectedImageIndex = imageIndex(c[j][5], false);
                    nn.Nodes.Add(nnn);
                }
                ag = functionService.shorts.a(p[i][1], ";", p[i][2], ";", p[i][5]); nn.Tag = ag; nn.ImageIndex = imageIndex(p[i][5], true); nn.SelectedImageIndex = imageIndex(p[i][5], false);
                tno.Nodes.Add(nn);
            }


            readTableChild_listView(listView1, indexSelectNode, objectCodParent, levo);


        }

        private void readTableChild_listView(ListView lv,
        int indexSelectNode, object objectCodParent, int levo)
        {
            if (levo == 2)
            {
                DataView p = new DataView(df);
                p.RowFilter = string.Concat("cop = ", objectCodParent, " and lev =", levo + 1);

                int ip = p.Count;
                DataView c = new DataView(df);
                string ag = "";

                string[] sa = new string[ip];

                lv.Items.Clear(); ListViewItem lvi;// = new ListViewItem();
                ListViewItem.ListViewSubItem lvis;
                string stn = "", stnn = "", stnk = ""; int ipo = 0;
                for (int i = 0; i < ip; i++)
                {
                    lvi = new ListViewItem();
                    stn = p[i][df_fields_name_3].ToString(); ipo = stn.IndexOf(" - "); stnn = stn.Remove(ipo);
                    lvi.Text = stnn; //p[i][df_fields_name_3].ToString().Remove();
                    //lvi.ImageIndex = Convert.ToInt32(p[i][df_fields_fo_5]);

                    lvis = new ListViewItem.ListViewSubItem();
                    stnk = stn.Substring(ipo + 3, 1);
                    lvis.Text = stnk;
                    lvi.SubItems.Add(lvis);

                    lvis = new ListViewItem.ListViewSubItem();
                    lvis.Text = "110";
                    lvi.SubItems.Add(lvis);

                    lvis = new ListViewItem.ListViewSubItem();
                    lvis.Text = "110";
                    lvi.SubItems.Add(lvis);
                    
                    
                    
                    //lv.Items.Add(p[i][df_fields_name_3].ToString(), imageIndex(p[i][df_fields_fo_5], true));
                    
                    ag = functionService.shorts.a(p[i][df_fields_lev_1], ";", p[i][df_fields_cod_2], ";", p[i][df_fields_fo_5]);
                    sa[i] = ag;
                    this.listView1.Items.Add(lvi);
                }
                //for (int i = 0; i < ip; i++)
                //{
                //    lv.Items[i].Tag = sa[i];
                //}
            }
        }

        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            afterExp();
        }

        string readTAG(string tago, int indexo)
        {
            string[] sa = tago.Split(';'); return sa[indexo];
        }
        string readTAG(object tago, int indexo)
        {
            string[] sa = tago.ToString().Split(';'); return sa[indexo];
        }
        int readTAGi(object tago, int indexo)
        {
            string[] sa = tago.ToString().Split(';'); return Convert.ToInt32(sa[indexo]);
        }


        private int imageIndex(object pi5, bool true_no_select)
        {
            int ir = 0;
            int[] aObj = new int[] { 10, 11 }; int[] aFld = new int[] { 8, 9 };
            if (pi5.ToString() == "1")
            {
                if (true_no_select) ir = aObj[0]; else ir = aObj[1];
            }
            else
            {
                if (true_no_select) ir = aFld[0]; else ir = aFld[1];
            }
            return ir;
        }
        private int imageIndexLV(object pi5, bool true_no_select)
        {
            int ir = 0;
            int[] aObj = new int[] { 0, 1 }; int[] aFld = new int[] { 0, 1 };
            if (pi5.ToString() == "1")
            {
                if (true_no_select) ir = aObj[0]; else ir = aObj[1];
            }
            else
            {
                if (true_no_select) ir = aFld[0]; else ir = aFld[1];
            }
            return ir;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            IsSelectNode = true;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
             addNodes_zak(treeView1.SelectedNode);
        }

        private void addNodes_zak(TreeNode tn)
        {
            int levelo = 0; 
            string scodParent = "888"; //readTAG(tn.Tag, iCOD_2); fs.shorts.m(tn.Tag);// q.readCod(tn.Text);
            int icob;// HT_level_codMAX(levelo + 1) + 1;
            string ss; //= q.name_cod(s, ic); //fs.shorts.m(ss);
            addToTable_zakaz(out ss, out icob);
            TreeNode tnn = new TreeNode(ss.Replace("z", "Заказ")); tnn.Tag = functionService.tagWork.tv_tag(1, icob, 0);//levelo + 1;
            tn.Nodes.Add(tnn);
        }
        private void addNodes_table(TreeNode tn)
        {
            int levelo = readTAGi(tn.Tag, iLEV_1);   int icob;
            object copo = readTAGi(tn.Tag, iCOD_2);  string ss="";
            addToTable_table(out ss, out icob, copo); //fs.shorts.m(ss);
            TreeNode tnn = new TreeNode(ss.Replace("p", "Место"));
            tnn.Tag = functionService.tagWork.tv_tag(2, icob, 0);
            tn.Nodes.Add(tnn);
        }
        private void addNodes_if_Load(TreeNode tn)
        {
            int levelo = readTAGi(tn.Tag, iLEV_1);
            //q.sh(((DataTable)aa[levelo + 1]));
            string scodParent = readTAG(tn.Tag, iCOD_2); ;// q.readCod(tn.Text);
            int ic = HT_level_codMAX(levelo + 1) + 1;
            string s = functionService.shorts.a(FOLDER, ic), ss = functionService.nameCod.name_cod(s, ic); //fs.shorts.m(ss);
            addToTable_AnyLevel(s, levelo + 1, scodParent);

            TreeNode tnn = new TreeNode(ss); tnn.Tag = levelo + 1;
            tn.Nodes.Add(tnn);
        }
        private void addToTable_zakaz(out string sNameo, out int icodo)
        {
            string sName = "Заказ"; object copo = "888";
            DataTable dto = df;
            DataRow r = dto.NewRow(); int ic = df_codMAX_lev(1) + 1; icodo = ic; sNameo = functionService.shorts.a("z_", ic.ToString().Substring(5, 3));
            r[0] = 0;
            r[1] = 1;
            r[2] = ic;
            r[3] = sNameo;
            r[4] = copo;
            r[5] = 0;
            dto.Rows.Add(r); dto.AcceptChanges();

            string sql_insert =
                string.Concat("INSERT INTO ", SCHEMA, ".", TABLENAME, "_folders(lev, cod, name, cop, fo) VALUES (1,", ic, ",'", sNameo, "',", copo, ", 0)");
            q.UpdateData(sql_insert);
        }
        private void addToTable_table(out string sNameo, out int icodo, object copo)
        {
            //object copo = icodo;
            DataTable dto = df;
            DataRow r = dto.NewRow(); int ic = df_codMAX_lev(2) + 1; icodo = ic; sNameo = functionService.shorts.a("p_", ic.ToString().Substring(4, 3));
            r[0] = 0;
            r[1] = 2;
            r[2] = ic;
            r[3] = sNameo;
            r[4] = copo;
            r[5] = 0;
            dto.Rows.Add(r); dto.AcceptChanges();

            string sql_insert =
                string.Concat("INSERT INTO ", SCHEMA, ".", TABLENAME,
                "_folders(lev, cod, name, cop, fo) VALUES (2,", ic, ",'", sNameo, "',", copo, ", 0)"); //fs.shorts.m(sNameo);
            q.UpdateData(sql_insert);
        }
        private void addToTable_AnyLevel(string sNameo, int lev, object copo)
        {
            DataTable dto = df;
            DataRow r = dto.NewRow(); int ic = df_codMAX_lev(lev) + 1;
            r[0] = 0;
            r[1] = lev;
            r[2] = ic;
            r[3] = sNameo;
            r[4] = copo;
            r[5] = 0;
            dto.Rows.Add(r); dto.AcceptChanges();

            string sql_insert =
                string.Concat("INSERT INTO ", SCHEMA, ".", TABLENAME, "_folders(lev, cod, name, cop, fo) VALUES (", lev, ",", ic, ",'", sNameo, "',", copo, ", 0)");
            q.UpdateData(sql_insert);
        }
        private int df_codMAX_lev(int levo)
        {
            DataView v = new DataView(df); v.RowFilter = string.Concat("lev=", levo);
            int ic = v.Count, im;

            if (ic > 0) { im = Convert.ToInt32(df.Compute("MAX(cod)", string.Concat("cod<>888 and lev=", levo))); return im; }
            else { return 0; }
        }
        private string FOLDER
        {
            get { return q.tk(param.default_3); }
        }
        private int HT_level_codMAX(int levelo)
        {
            DataView p = new DataView(df); p.RowFilter = functionService.shorts.a("lev =", levelo); int ic = p.Count;

            if (ic > 0)
            {
                int im;   //dt.Rows.Count, im;
                im = Convert.ToInt32(df.Compute("MAX(cod)", functionService.shorts.a("lev=", levelo))); return im;

            }
            else
            {
                return 0;
            }

        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            addNodes_table(treeView1.SelectedNode);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        
    }
}
